#include<pthread.h>
#include<iostream>
#include<string>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
int flag=0;
//可重入函数：
 std::string getidformat(const pthread_t i){
        char buff[1024]={0};//局部变量
        sprintf(buff,"0x%lx",i);
        return buff;
 }
void * routine(void*arg){
   pthread_t id= pthread_self();
     std::string name=static_cast<const char*>(arg);
     int cnt=5;
     while(cnt--){
     sleep(1);
     flag++;
       
     std::cout<<"线程:"<<name<<"线程id："<< getidformat(id)<<std::endl;
     pthread_exit((void*)&flag);
     }
     return (void*) 1;
     
}
int main(){
    pthread_t tid;
    //n成功返回0失败则返回去其他
    int n=pthread_create(&tid,nullptr,routine,(void*)"thread_1");
    pthread_t myid= pthread_self();
    sleep(1);
    void *ret=nullptr;
      pthread_join(tid,&ret);//阻塞等待子线程
      std::cout<<*(int*)ret<<std::endl;

       while(1){
        sleep(1);
      std::cout<<"进程pid:"<<getpid()<< " 我是主线程,我的线程id："<<getidformat(myid)<<" 子线程id: "<<
      getidformat(tid)<<" "<<flag<<std::endl;
       }

 
      
}